package com.evangelion.web.filter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.evangelion.core.bean.Seccione;
import com.evangelion.core.bean.Usuario;


/**
 * Date: 2/17/14
 */
public class SecurityUtils {

    public static Usuario getUserFromSession(HttpServletRequest request) {
        return getUserFromSession(request.getSession(false));
    }

    public static Usuario getUserFromSession(HttpSession session) {
        if(null == session) {
            return null;
        }

        Usuario user = (Usuario) session.getAttribute("UserSession");

        if (user == null) {
            return null;
        } else {
            return user;
        }
    }


    public static void addUserToSession(HttpSession session, Usuario user) {
        session.setAttribute("UserSession", user);
    }


    public static void addUserToSession(HttpServletRequest req, Usuario user) {
        req.getSession(true).setAttribute("UserSession", user);
    }


    public static void setSeccion(HttpServletRequest request, Seccione seccion) {
        HttpSession httpSession = request.getSession(false);
        httpSession.setAttribute("seccion", seccion);

    }


    public static Seccione getSeccion(HttpSession httpSession) {
        return (Seccione) httpSession.getAttribute("seccion");
    }

    public static Seccione getSeccion(HttpServletRequest request) {
        if(null != request.getSession(false)) {
            return (Seccione) request.getSession().getAttribute("seccion");
        }
        return null;
    }
}
